package in.muton.edushare.pages.univ;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.PageRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.components.images.ProfileImage;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.cp.semCP.SemesterCP;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.summary.html.UnivSummary;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.vo.Universities;

public abstract class UnivBase extends EduShareBasePage {

	protected static String[] menuItems = { "UnivUpdates", "folders", "UnivCalendar",
			"UnivAcadProgs", "UnivStudents", "UnivProfs", "UnivAdmins",
			"UnivEveryone", "UnivDept", "UnivInvite", "UnivCourse",
			"UnivControlPanel", "UnivProfile", "DeptCreate" ,"UnivPoll","UnivBatches",
			"SemesterControl", "UnivGroups"/*, "createBulkStudent"*/};

	public UnivBase(PageParameters params) {
		super();
		add(CSSPackageResource.getHeaderContribution(UnivBase.class,
				"UnivPage.css"));
		setUniversity(params);
		ud = new UnivDataHandler(getModel());
		add(new ProfileImage("univIcon", getModel(), ApplicationConstants.FILE_SUFFIX_ICON));
		add(new Label("name", getModel().getObject().getUniversityName()));
		add(new BookmarkablePageLink<Void>("vwProfile", URLLoader.getPageClass(URLConstants.UNIV_PUBLIC_PAGE_HOME), EntityPageParams.getParams(getModel())));
		add(new Link<Void>("printReport"){

			private static final long serialVersionUID = 563340255538035753L;

			@Override
			public void onClick() {
				setResponsePage(new UnivSummary(UnivBase.this.model));
			}});
		setMenuItems(params);
	}

	private void setMenuItems(PageParameters p) {
		String highlight = this.getActivePage(p);
		UnivRoleCheck u = new UnivRoleCheck(getModel().getObject(), getLoggedInUser());
		for (String s : menuItems) {
			WebMarkupContainer wmc;
			add(wmc = new WebMarkupContainer(s));
			EntityPageParams.getParams(getModel(), s);
			if (highlight.compareTo(s) == 0) {
				wmc.add(new AttributeAppender("class", new Model<String>(
						"es-selected"), " "));
			}
			if (s.compareTo("UnivUpdates") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", UnivPage.class,
						EntityPageParams.getParams(getModel())));
			} else if(s.equals("folders")){
				wmc.add(new BookmarkablePageLink<Void>("link", UnivFolders.class,
						EntityPageParams.getParams(getModel())));
			}else if(s.equals("UnivBatches")){
				wmc.add(new BookmarkablePageLink<Void>("link", UnivBatches.class,
						EntityPageParams.getParams(getModel())));
			}else if (s.compareTo("UnivCalendar") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", UnivCalendar.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("UnivAcadProgs") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", UnivAcadProgs.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("UnivStudents") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("UnivProfs") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("UnivAdmins") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("UnivEveryone") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivPeople.class, EntityPageParams.getParams(
								getModel(), s)));
			} else if (s.compareTo("UnivDept") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", UnivDept.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.compareTo("UnivCourse") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivCourses.class, EntityPageParams
								.getParams(getModel())));
			} else if (s.compareTo("UnivControlPanel") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivSetting.class, EntityPageParams
								.getParams(getModel())));
				wmc.setVisible(false);
			} else if (s.compareTo("UnivProfile") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link", UnivEdit.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(u.isAdmin());
			} else if (s.compareTo("UnivInvite") == 0) {
				wmc.setVisible(u.isAdmin());
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivInvite.class, EntityPageParams
								.getParams(getModel())));
			} else if (s.compareTo("DeptCreate") == 0) {
				wmc.add(new BookmarkablePageLink<Void>("link",
								DeptCreate.class, EntityPageParams.getParams(getModel())));
				wmc.setVisible(u.isAdmin());
			}else if(s.equals("UnivPoll")){
				wmc.add(new BookmarkablePageLink<Void>("link",
					UnivPoll.class, EntityPageParams.getParams(getModel())));				
				wmc.setVisible(u.isAdmin()||u.isTeacher());
			}else if(s.equals("SemesterControl")){
				wmc.add(new BookmarkablePageLink<Void>("link", SemesterCP.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(u.isAdmin());
			}else if(s.equals("UnivGroups")){
				wmc.add(new BookmarkablePageLink<Void>("link",
						UnivGroups.class, EntityPageParams.getParams(getModel())));				
			}/*else if(s.equals("createBulkStudent")){
				wmc.add(new BookmarkablePageLink<Void>("link",
						CreateBulkStudent.class, EntityPageParams.getParams(getModel())));	
				wmc.setVisible(u.isAdmin());
			}*/
			
		}
	}

	public abstract String getActivePage(PageParameters p);

	private void setUniversity(PageParameters params) {
		if (params == null) {
			throw new EduShareRuntimeException("Unknown University Page");
		}
		String uniqueName = params.getString("0");
		Universities univ = new UniversitiesDAO(getSessionORMInterface())
				.getUnivById(uniqueName);
		if (univ == null) {
			throw new EduShareRuntimeException("Unknown University Page");
		}
		model = new CayenneModel<Universities>(univ);
		prc = new PageRoleCheck(univ, getLoggedInUser());
	}

	@Override
	public String getUserRole() {
		return " : " + ud.getUserRole(EduShareSession.get().getLoggedInUser());
	}

	public void setModel(IModel<Universities> model) {
		this.model = model;
	}

	public IModel<Universities> getModel() {
		return model;
	}

	private IModel<Universities> model = null;
	protected UnivDataHandler ud = null;
}
